package cd.cdyb.sms.utils;

import cd.cdyb.sms.domain.OssConfig;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;

import java.io.InputStream;

/**
 * @Author: lilin
 * @CreateTime: 2023-07-27  16:47
 * @Description: TODO
 */
public class OssFileUtils {

    public static String uploadByAliYun(InputStream inputStream, OssConfig oosConfig, String fileName, String contentType) throws Exception {
        /**判断文件大小*/
        //开始压缩图片 压缩图片耗时太长

        //上传到阿里云
        OSSClient ossClient = new OSSClient(oosConfig.getEndpoint(), oosConfig.getAccessKeyId(), oosConfig.getAccessKeySecret());
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(inputStream.available());
        objectMetadata.setCacheControl("no-cache");
        objectMetadata.setHeader("Pragma", "no-cache");
        objectMetadata.setContentType(getcontentType(contentType));
        objectMetadata.setContentDisposition("inline;filename=" + fileName);

        // 指定上传文件操作时是否覆盖同名Object。
        // 不指定x-oss-forbid-overwrite时，默认覆盖同名Object。
        // 指定x-oss-forbid-overwrite为false时，表示允许覆盖同名Object。
        // 指定x-oss-forbid-overwrite为true时，表示禁止覆盖同名Object，如果同名Object已存在，程序将报错。
        objectMetadata.setHeader("x-oss-forbid-overwrite", "false");
        PutObjectRequest putObjectRequest = new PutObjectRequest(oosConfig.getBucketName(), fileName, inputStream);
        putObjectRequest.setMetadata(objectMetadata);

        // 上传文件。
        ossClient.putObject(putObjectRequest);
        //ossClient.putObject(oosConfig.getBucketName(), fileName, instream,objectMetadata);
        ossClient.shutdown();
        return "http://" + oosConfig.getBucketName() + "." + oosConfig.getEndpoint() + "/" + fileName;
    }


    /**
     * Description: 判断OSS服务文件上传时文件的contentType
     *
     * @param FilenameExtension 文件后缀
     * @return String
     */
    public static String getcontentType(String FilenameExtension) {
        if ("bmp".equalsIgnoreCase(FilenameExtension)) {
            return "image/bmp";
        }
        if ("gif".equalsIgnoreCase(FilenameExtension)) {
            return "image/gif";
        }
        if ("jpeg".equalsIgnoreCase(FilenameExtension) ||
                "jpg".equalsIgnoreCase(FilenameExtension) ||
                "png".equalsIgnoreCase(FilenameExtension)) {
            return "image/jpeg";
        }
        if ("html".equalsIgnoreCase(FilenameExtension)) {
            return "text/html";
        }
        if ("txt".equalsIgnoreCase(FilenameExtension)) {
            return "text/plain";
        }
        if ("vsd".equalsIgnoreCase(FilenameExtension)) {
            return "application/vnd.visio";
        }
        if ("pptx".equalsIgnoreCase(FilenameExtension) ||
                "ppt".equalsIgnoreCase(FilenameExtension)) {
            return "application/vnd.ms-powerpoint";
        }
        if ("docx".equalsIgnoreCase(FilenameExtension) ||
                "doc".equalsIgnoreCase(FilenameExtension)) {
            return "application/msword";
        }
        if ("xml".equalsIgnoreCase(FilenameExtension)) {
            return "text/xml";
        }
        return "image/jpeg";
    }

}
